Wiimote1.led1 = true
Wiimote2.led1 = true
Wiimote2.led4 = true
Wiimote3.led2 = true
Wiimote4.led2 = true
Wiimote4.led4 = true
Wiimote5.led3 = true
Wiimote6.led3 = true
Wiimote6.led4 = true
//Mouse Control Script using IR
//by vkapadia with much assistance from inio
//vkapadia@vkapadia.com
//
//Calibration:
//To calibrate, run this program and put the Wiimote on a flat surface face-up.
//Then read the values in the debug line (next to the run button).
//Change these values until the debug line reads approx. all zeros.
var.xtrim1 = -1
var.ytrim1 = -25
var.ztrim1 = 2

var.xtrim2 = 2
var.ytrim2 = -30
var.ztrim2 = 2

var.coeff =  0.1

//
//Options:
var.deadzone = 5 //distance in pixels that you have to move the wiimote in
   //order for it to register movement. Creates a "dead zone" around the pointer
   //to make it easier to click. Higher = smoother but less accurate.
//fake cursor init

//cursor2.visible = true

//more options to be added later

//Controls:
//Point Wiimote = Move Mouse
//D-Pad = Arrow Keys
//B-Button = Left Click
//Home = Middle Click
//A-Button = Right Click
//Plus and Minus = Control Volume
//One = Unmapped
//Two = Unmapped
//
//If the pointer hits the edge of the screen, the Wiimote will rumble a bit.
//
//The LEDs attempt to emulate KITT's grill from Knight Rider

//WIIMOTE 1
//***Do not edit anything below this line unless you know what you are doing.***
var.accx1 = wiimote1.RawForceX + var.xtrim1
var.accy1 = wiimote1.RawForceY + var.ytrim1
var.accz1 = wiimote1.RawForceZ + var.ztrim1

if wiimote1.dot1vis and wiimote1.dot2vis then

  if var.accy1 > -7 then
    var.orientation1 = 0
  elseif var.accy1 > -45 then
    if var.accx1 < 0 then
      var.orientation1 = 3
    else
      var.orientation1 = 1
    endif
  else
    var.orientation1 = 2
  endif

  if var.leftpoint1 = 0 then
    if var.orientation1 = 0 then
      if wiimote1.dot1x < wiimote1.dot2x then
        var.leftpoint1 = 1
      else
        var.leftpoint1 = 2
      endif
    endif
    if var.orientation1 = 1 then
      if wiimote1.dot1y > wiimote1.dot2y then
        var.leftpoint1 = 1
      else
        var.leftpoint1 = 2
      endif
    endif
    if var.orientation1 = 2 then
      if wiimote1.dot1x > wiimote1.dot2x then
        var.leftpoint1 = 1
      else
        var.leftpoint1 = 2
      endif
    endif
    if var.orientation = 3 then
      if wiimote1.dot1y < wiimote1.dot2y then
        var.leftpoint1 = 1
      else
        var.leftpoint1 = 2
      endif
    endif
  endif

  if var.leftpoint1 = 1 then
    var.fix1x1 = wiimote1.dot1x
    var.fix1y1 = wiimote1.dot1y
    var.fix2x1 = wiimote1.dot2x
    var.fix2y1 = wiimote1.dot2y
  else
    var.fix1x1 = wiimote1.dot2x
    var.fix1y1 = wiimote1.dot2y
    var.fix2x1 = wiimote1.dot1x
    var.fix2y1 = wiimote1.dot1y
  endif

  var.dx1 = var.fix2x1 - var.fix1x1
  var.dy1 = var.fix2y1 - var.fix1y1
  var.cx1 = (var.fix1x1+var.fix2x1)/1024.0 - 1
  var.cy1 = (var.fix1y1+var.fix2y1)/1024.0 - .75

  var.d1 = sqrt(var.dx1*var.dx1+var.dy1*var.dy1)

  var.dx1 = var.dx1 / var.d1
  var.dy1 = var.dy1 / var.d1

  var.ox1 = -var.dy1*var.cy1-var.dx1*var.cx1;
  var.oy1 = -var.dx1*var.cy1+var.dy1*var.cx1;

  var.ax1 = (var.ox1 * var.coeff*screen.desktopwidth) + (screen.desktopwidth* var.coeff / 2)
  var.ay1 = (-var.oy1* var.coeff * screen.desktopwidth) + (screen.desktopheight* var.coeff / 2)

  var.dx1 = var.ax1 - cursor2.posx
  var.dy1 = var.ay1 - cursor2.posy

  var.d1 = sqrt((var.dx1*var.dx1)+(var.dy1*var.dy1))

  var.a1 = 180 / (200 + var.d1 * var.d1 * var.d1 * .001)

  if var.d1 <= var.deadzone then var.a1 = 1

  //debug = var.d + " " + var.a

  var.finalx1 = cursor2.posx * var.a1 + var.ax1 * (1 - var.a1)
  var.finaly1 = cursor2.posy * var.a1 + var.ay1 * (1 - var.a1)


  cursor2.posx = smooth(var.finalx1,3,5)
  cursor2.posy = smooth(var.finaly1,3,5)

else

  var.leftpoint1 = 0

endif

var.xpos1 = var.finalx1
var.ypos1 = var.finaly1
ppjoy1.analog0 = ensuremaprange(var.xpos1,0,screen.desktopwidth* var.coeff,-1,1)
ppjoy1.analog1 = ensuremaprange(var.ypos1,0,screen.desktopheight* var.coeff,-1,1)

if wiimote1.B or wiimote1.A or wiimote1.Up or wiimote1.down or wiimote1.Left or wiimote1.Right == true
  ppjoy1.digital0 = true
else
  ppjoy1.digital0 = false
endif

//WIIMOTE 2
//***Do not edit anything below this line unless you know what you are doing.***
var.accx2 = wiimote2.RawForceX + var.xtrim2
var.accy2 = wiimote2.RawForceY + var.ytrim2
var.accz2 = wiimote2.RawForceZ + var.ztrim2

if wiimote2.dot1vis and wiimote2.dot2vis then

  if var.accy2 > -7 then
    var.orientation2 = 0
  elseif var.accy2 > -45 then
    if var.accx2 < 0 then
      var.orientation2 = 3
    else
      var.orientation2 = 1
    endif
  else
    var.orientation2 = 2
  endif

  if var.leftpoint2 = 0 then
    if var.orientation2 = 0 then
      if wiimote2.dot1x < wiimote2.dot2x then
        var.leftpoint2 = 1
      else
        var.leftpoint2 = 2
      endif
    endif
    if var.orientation2 = 1 then
      if wiimote2.dot1y > wiimote2.dot2y then
        var.leftpoint2 = 1
      else
        var.leftpoint2 = 2
      endif
    endif
    if var.orientation2 = 2 then
      if wiimote2.dot1x > wiimote2.dot2x then
        var.leftpoint2 = 1
      else
        var.leftpoint2 = 2
      endif
    endif
    if var.orientation = 3 then
      if wiimote2.dot1y < wiimote2.dot2y then
        var.leftpoint2 = 1
      else
        var.leftpoint2 = 2
      endif
    endif
  endif

  if var.leftpoint2 = 1 then
    var.fix1x2 = wiimote2.dot1x
    var.fix1y2 = wiimote2.dot1y
    var.fix2x2 = wiimote2.dot2x
    var.fix2y2 = wiimote2.dot2y
  else
    var.fix1x2 = wiimote2.dot2x
    var.fix1y2 = wiimote2.dot2y
    var.fix2x2 = wiimote2.dot1x
    var.fix2y2 = wiimote2.dot1y
  endif

  var.dx2 = var.fix2x2 - var.fix1x2
  var.dy2 = var.fix2y2 - var.fix1y2
  var.cx2 = (var.fix1x2+var.fix2x2)/1024.0 - 1
  var.cy2 = (var.fix1y2+var.fix2y2)/1024.0 - .75

  var.d2 = sqrt(var.dx2*var.dx2+var.dy2*var.dy2)

  var.dx2 = var.dx2 / var.d2
  var.dy2 = var.dy2 / var.d2

  var.ox2 = -var.dy2*var.cy2-var.dx2*var.cx2;
  var.oy2 = -var.dx2*var.cy2+var.dy2*var.cx2;

  var.ax2 = (var.ox2 * var.coeff*screen.desktopwidth) + (screen.desktopwidth* var.coeff / 2)
  var.ay2 = (-var.oy2* var.coeff * screen.desktopwidth) + (screen.desktopheight* var.coeff / 2)

  var.dx2 = var.ax2 - cursor3.posx
  var.dy2 = var.ay2 - cursor3.posy

  var.d2 = sqrt((var.dx2*var.dx2)+(var.dy2*var.dy2))

  var.a2 = 180 / (200 + var.d2 * var.d2 * var.d2 * .001)

  if var.d2 <= var.deadzone then var.a2 = 1

  //debug = var.d + " " + var.a

  var.finalx2 = cursor3.posx * var.a2 + var.ax2 * (1 - var.a2)
  var.finaly2 = cursor3.posy * var.a2 + var.ay2 * (1 - var.a2)


  cursor3.posx = smooth(var.finalx2,3,5)
  cursor3.posy = smooth(var.finaly2,3,5)

else

  var.leftpoint2 = 0

endif

var.xpos2 = var.finalx2
var.ypos2 = var.finaly2
ppjoy2.analog0 = ensuremaprange(var.xpos2,0,screen.desktopwidth* var.coeff,-1,1)
ppjoy2.analog1 = ensuremaprange(var.ypos2,0,screen.desktopheight* var.coeff,-1,1)

if wiimote2.B or wiimote2.A or wiimote2.Up or wiimote2.down or wiimote2.Left or wiimote2.Right == true
  ppjoy2.digital0 = true
else
  ppjoy2.digital0 = false
endif

debug = var.accx2+ "  " + var.accy2+ " " + var.accz2